package org.dk.poitalk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.dk.poitalk.model.Reward;
import org.dk.poitalk.service.reward.RewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.LocaleResolver;

import com.jappstart.model.auth.UserAccount;
import com.jappstart.service.auth.EnhancedUserDetailsService;

@Controller
@RequestMapping("/reward")
@SessionAttributes(RewardController.REWARD)
public class RewardController {

	private static final Logger logger = Logger.getLogger(RewardController.class.getName());

	protected static final String REWARD = "reward";

	private RewardService rewardService;

	private LocaleResolver localeResolver;

	private EnhancedUserDetailsService userDetailsService;

	private MessageSource messageSource;

	public RewardService getRewardService() {
		return rewardService;
	}

	@Autowired
	public void setRewardService(RewardService rewardService) {
		this.rewardService = rewardService;
	}

	public final LocaleResolver getLocaleResolver() {
		return localeResolver;
	}

	@Autowired
	public final void setLocaleResolver(final LocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}

	public final EnhancedUserDetailsService getUserDetailsService() {
		return userDetailsService;
	}

	@Autowired
	public final void setUserDetailsService(final EnhancedUserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}	

	public MessageSource getMessageSource() {
		return messageSource;
	}

	@Autowired
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	@RequestMapping(value = "/reward", method = RequestMethod.GET)
    public final String create() {
        return "reward";
    }

    @RequestMapping(value = "/addx", method = RequestMethod.POST)
    @ResponseBody
    public final Map<String, Boolean> addx(@RequestBody final Map<String, Object> request) {
        final Map<String, Boolean> response = new HashMap<String, Boolean>();
        logger.info("add");
        Reward reward = new Reward();
        reward.setName(request.get("name").toString());
        reward.setUserAccount(new UserAccount(((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername()));
		this.rewardService.saveOrUpdate(reward);
        return response;
    }
    
    @RequestMapping(value="/searchx", method= RequestMethod.POST)
    public @ResponseBody DataTablesResponse<Map<String, Object>> searchx(@RequestBody DataTablesRequest dataTablesRequest, HttpServletRequest request, HttpServletResponse response) {
    	DataTablesResponse<Map<String, Object>> dataTablesResponse = new DataTablesResponse<Map<String, Object>>();
    	logger.info("search");
    	Map<String, Object> mapValue = new HashMap<String, Object>();
    	List<Reward> rewardList = this.rewardService.search("Reward.findAll", mapValue);
    	List<Map<String,Object>> listDataTable = new ArrayList<Map<String, Object>>();
    	if (rewardList != null) {
        	for (Reward reward : rewardList) {
        		Map<String, Object> mapDataRows = new HashMap<String, Object>();
        		mapDataRows.put("key", reward.getKey() != null ? reward.getKey().toString() : StringUtils.EMPTY);
        		mapDataRows.put("name", reward.getName());
        		mapDataRows.put("username", reward.getUserAccount() != null ? reward.getUserAccount().getUsername() : StringUtils.EMPTY);
        		listDataTable.add(mapDataRows);
			}
        }
    	dataTablesResponse.echo = dataTablesRequest.echo;
    	dataTablesResponse.totalRecords = listDataTable.size();
    	dataTablesResponse.totalDisplayRecords = listDataTable.size();
    	int endSubList = ((dataTablesRequest.displayStart+dataTablesRequest.displayLength)>listDataTable.size())?listDataTable.size():(dataTablesRequest.displayStart+dataTablesRequest.displayLength);
    	dataTablesResponse.data = listDataTable.subList(dataTablesRequest.displayStart, endSubList);
    	dataTablesResponse.columns = "{key,name,username}";
        return dataTablesResponse;
    }
      
}
